#coding:utf-8

import report
import buss
from protocols import *
import protocols
import wx
from gui import customwid as wid
import sys
import datetime
        
class Render(buss.BaseRender):
    def init(self):
        self._excelName = ''
        self.results = []
        self.initPars()
        
    def initPars(self):
        
        """
        1. 请求时间, 2.应答时间, 3. 传输时间, 4.完成时间, 5.大小, 6.备注.
        """
        if hasattr(self, '_request') and self._request:
            self.results.append([self._request, self._response, self._startTrans,
                             self._complete, self._photoSize, self._remark])
        self._request = ''
        self._response = ''
        self._startTrans = ''
        self._complete = ''
        self._remark = {}
        self._photoSize = 0
        self._packages = set()
        self._total = 0
        self._curIndex = None

    def exportExcel(self, xlSheet):
        xlSheet.name = self._excelName
        content = ((u"次数", "", 10),
                   (u"请求抓拍时间", "hh:mm:ss", 10), 
                    (u"终端应答时间", "hh:mm:ss", 10), 
                    (u"请求响应时间(秒)", "", 10),
                    (u"图片起始上传时间", "hh:mm:ss", 10),
                    (u"图片上传结束时间", "hh:mm:ss", 10),
                    (u"图片上传时间(秒)", "", 10),
                    (u"图片大小(KB)", "", 10),
                    (u"上传速率(KB/S)", "", 10),
                    )

        for i in range(1, 9):
            xlSheet.Columns(i).Font.Size = 9
            xlSheet.Cells(1, i).Value = content[(i - 1)][0]
            xlSheet.Columns(i).NumberFormat = content[(i - 1)][1]
            xlSheet.Columns(i).ColumnWidth = content[(i - 1)][2]
            xlSheet.Cells(1, i).Font.Bold = True
        
        curRow = 1
        for result in self.results:
            curRow += 1
            xlSheet.Cells(curRow, 1).Value = str(curRow-1)
            xlSheet.Cells(curRow, 2).Value = result[0]
            xlSheet.Cells(curRow, 3).Value = result[1]
            
            try:
                k = result[0]
                time1 = datetime.datetime(2000,1,1,int(k[11:13]), int(k[14:16]), int(k[17:19]))
                k = result[1]
                time2 = datetime.datetime(2000,1,1,int(k[11:13]), int(k[14:16]), int(k[17:19]))
                xlSheet.Cells(curRow, 4).Value = str((time2-time1).seconds)
                xlSheet.Cells(curRow, 5).Value = result[2]
                xlSheet.Cells(curRow, 6).Value = result[3]
                k = result[2]
                time1 = datetime.datetime(2000,1,1,int(k[11:13]), int(k[14:16]), int(k[17:19]))
                k = result[3]
                time2 = datetime.datetime(2000,1,1,int(k[11:13]), int(k[14:16]), int(k[17:19]))
                xlSheet.Cells(curRow, 7).Value = str((time2-time1).seconds)
                xlSheet.Cells(curRow, 8).Value = "%.2f"%(result[4]/1024.0)
            except:
                pass
        
    def show(self):
        self.initPars()
        ctrl = wx.ListCtrl(self.getParent(), style=wx.LC_REPORT)
        x = [u'请求时间', u'应答时间', u'传输时间', u'完成时间', u'图片大小(B)', u'备注']
        ctrl.InsertColumn(0, u'id',width=60)
        for i in range(len(x)):
            ctrl.InsertColumn(i+1, x[i],width=160)
        di = 0
        for result in self.results:
            index = ctrl.InsertStringItem(sys.maxint, str(di))
            for i in range(len(result)-1):
                ctrl.SetStringItem(index, i+1, str(result[i]))
            x = u''
            for key, value in result[5].items():
                x += u'%s:%s,'%(key, value)
            ctrl.SetStringItem(index, 6, x)
            di += 1
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(ctrl, proportion = 1, flag=wx.EXPAND)
        return sizer
                
    def p_4101(self, data):
        p = Protocol4101(data)
        self.initPars()
        self._excelName = "%s %s"%(p.getResolving(), p.getQuality())
        self._resolution = p.resolution
        self._quality = p.quality
        self._request = self.getTime()
        
    def p_4141(self, data):
        p = Protocol4141(data)
        if not p.getResponse():
            self._remark[u'拍照应答'] = u"失败"
        self._response = self.getTime()
            
    def p_4150(self, data):
        try:
            p = Protocol4150(data)
        except:
            print data, self.getTime()
        if p.getPackageNumber() == 0x00:
            if p.resolution != self._resolution:
                self._remark[u'分辨率'] = u"错误"
            if p.quality != self._quality:
                self._remark[u'质量'] = u"错误"
            self._startTrans = self.getTime()
        curIndex = p.getImageIndex()
        if self._curIndex is None:
            self._curIndex = curIndex
        if curIndex == self._curIndex:
            self._total = p.getPackageSum()
            self._packages.add((p.getPackageNumber(), p.getDataLength()))
            if len(self._packages) == self._total:
                self._complete = self.getTime()
                for i in self._packages:
                    self._photoSize += i[1]
                self.initPars()
        